package com.Lock;

import java.util.concurrent.TimeUnit;

/*
* Synchronized
* */
public class Demo01 {

    public static void main(String[] args) {
        Phone phone = new Phone();

        /*
        * 就是说这里，先执行sms，跟对象加锁，然后执行call的时候也会加锁，
        * 这就是可重入锁
        * */

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            phone.sms();
        },"one").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            phone.sms();
        },"two").start();

    }
}

class Phone{

    public synchronized void sms(){
        System.out.println(Thread.currentThread().getName()+"sms");
        call();//这里也有锁
    }

    public synchronized void call(){
        System.out.println(Thread.currentThread().getName()+"call");
    }
}
